<template>
	<div class="mod-config">
		<el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
			<el-form-item><el-input v-model="dataForm.key" placeholder="电话/名字" clearable></el-input></el-form-item>
			<el-form-item>
				<el-button @click="getDataList()">查询</el-button>
				<!-- <el-button :disabled="buttonDisable" type="primary" @click="addOrUpdateHandle()">修改预定</el-button> -->
				<el-button :disabled="buttonDisable" type="danger" @click="cancelReserve()">取消预定</el-button>
				<el-button :disabled="buttonDisable" type="success" @click="consumerToStore()">顾客到店</el-button>
			</el-form-item>
		</el-form>
		<el-table highlight-current-row="true" :data="dataList" border v-loading="dataListLoading" @row-click="handClick" style="width: 100%;">
			<el-table-column prop="reserveNo" header-align="center" align="center" label="预定编号"></el-table-column>
			<el-table-column prop="customerName" header-align="center" align="center" label="消费者姓名"></el-table-column>
			<el-table-column prop="customerTel" header-align="center" align="center" label="电话"></el-table-column>
			<el-table-column prop="peopleNum" header-align="center" align="center" label="人数"></el-table-column>
			<el-table-column prop="time" header-align="center" align="center" label="时间"></el-table-column>
			<el-table-column prop="reserveTime" header-align="center" align="center" label="预定时间"></el-table-column>
			<!-- <el-table-column prop="keepTime" header-align="center" align="center" label="预定保留时间"></el-table-column> -->
			<el-table-column prop="deposit" header-align="center" align="center" label="押金"></el-table-column>
			<el-table-column width="250" prop="foodList" header-align="center" align="center" label="菜品集合">
				<template slot-scope="scope">
					<div v-for="item in JSON.parse(scope.row.foodList)">
						{{ item.foodName }}/{{ item.foodNum }}份
						<font style="color: red;">￥{{ item.total }}元</font>
					</div>
				</template>
			</el-table-column>
			<el-table-column prop="diningTableNo" header-align="center" align="center" label="桌号"></el-table-column>
			<el-table-column prop="totalMoney" header-align="center" align="center" label="总价"></el-table-column>
			<el-table-column prop="status" header-align="center" align="center" label="状态">
				<template slot-scope="scope">
					<el-tag type="success" v-show="scope.row.status === 1">预定中</el-tag>
					<el-tag type="danger" v-show="scope.row.status === 2">已到店</el-tag>
					<el-tag type="info" v-show="scope.row.status === 3">已取消</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="remark" header-align="center" align="center" label="备注"></el-table-column>
		</el-table>
		<el-pagination
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
			:current-page="pageIndex"
			:page-sizes="[10, 20, 50, 100]"
			:page-size="pageSize"
			:total="totalPage"
			layout="total, sizes, prev, pager, next, jumper"
		></el-pagination>
		<!-- 弹窗, 新增 / 修改 -->

		<el-dialog title="预定详情" :visible.sync="addOrUpdateVisible" append-to-body :close-on-click-modal="false" width="50%" center>
			<add-or-update ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
		</el-dialog>

		<el-dialog title="顾客到店" :visible.sync="consumerToStoreVisible" append-to-body :close-on-click-modal="false" width="30%" center>
			<div style="text-align: center;font-size: 1.25rem">{{ deskStatusDetail }}</div>
			<br />
			<br />
			<div style="text-align: center;">
				<el-button v-if="reserveInfo.deskStatus === 1" type="primary" @click="submit">确定</el-button>
				<el-button type="success" @click="turnDesk">更换餐台</el-button>
				<el-button type="warning" @click="consumerToStoreVisible = false">取消</el-button>
			</div>
		</el-dialog>

		<!-- 更换餐台 -->
		<SeatCom @refreshData="getDataList" v-if="seatComVisile" ref="SeatComRef"></SeatCom>
	</div>
</template>

<script>
import AddOrUpdate from './tstorereservedesk-add-or-update';
import SeatCom from './SeatComponent.vue';
export default {
	data() {
		return {
			dataForm: {
				key: ''
			},
			dataList: [],
			pageIndex: 1,
			pageSize: 10,
			totalPage: 0,
			dataListLoading: false,
			dataListSelections: [],
			addOrUpdateVisible: false,
			buttonDisable: true,
			consumerToStoreVisible: false,
			reserveInfo: {},
			deskStatusDetail: '',
			seatComVisile: false,
			orderInfo: {}
		};
	},
	components: {
		AddOrUpdate,
		SeatCom
	},
	methods: {
		//确定
		submit() {
			if(this.reserveInfo.foodList !== null){
				var foodVos = JSON.parse(this.reserveInfo.foodList);
				this.reserveInfo.foodVos = foodVos;
			}else{
				this.reserveInfo.foodVos = []
			}

			this.$http({
				url: this.$http.adornUrl(`/reception/tstorereservedesk/openDesk`),
				method: 'post',
				data: this.$http.adornData({
					peopleNum: this.reserveInfo.peopleNum,
					foodVos: this.reserveInfo.foodVos,
					id: this.reserveInfo.id,
					diningTableNo: this.reserveInfo.diningTableNo,
					totalMoney: this.reserveInfo.totalMoney,
					deskId: this.reserveInfo.deskId
				})
			}).then(({ data }) => {
				if (data && data.code === 0) {
					this.$message({
						message: '操作成功',
						type: 'success',
						duration: 1500
					});
					this.consumerToStoreVisible = false;
					this.getDataList()
				} else {
					this.$message.error(data.msg);
				}
			});
		},

		//转台
		turnDesk() {
			this.seatComVisile = true;
			this.$nextTick(() => {
				this.$refs.SeatComRef.init(this.reserveInfo);
			});
		},

		//顾客到店
		consumerToStore() {
			this.consumerToStoreVisible = true;
      console.log("info  "+JSON.stringify(this.reserveInfo))
			//桌号是否被占用
			if (this.reserveInfo.deskStatus === 1) {
				this.deskStatusDetail = '当前餐台处于空闲状态，可直接开台，也可更换餐台';
			}
			if (this.reserveInfo.deskStatus === 2 || this.reserveInfo.deskStatus === 3 || this.reserveInfo.deskStatus === 5) {
				this.deskStatusDetail = '当前餐台处于就餐，不可开台，可选择更换餐台';
			}
		},

		handClick(row, column, event) {
			if(row.status === 1){
				this.reserveInfo = row;
				this.buttonDisable = false;
			}else{
				this.buttonDisable = true
			}

		},

		cancelReserve(){
			this.$http({
				url: this.$http.adornUrl('/reception/tstorereservedesk/cancelReserve'),
				method: 'get',
				params: this.$http.adornParams({
					id:this.reserveInfo.id,
					status: 3,//取消预定
				})
			}).then(({ data }) => {
				if (data && data.code === 0) {
					this.getDataList()
				}
			});
		},

		// 获取数据列表
		getDataList() {
			this.seatComVisile = false
			this.dataListLoading = true;
			this.consumerToStoreVisible = false
			this.$http({
				url: this.$http.adornUrl('/reception/tstorereservedesk/list'),
				method: 'get',
				params: this.$http.adornParams({
					current: this.pageIndex,
					size: this.pageSize,
					key: this.dataForm.key
				})
			}).then(({ data }) => {
				if (data && data.code === 0) {
					this.dataList = data.page.records;
					this.totalPage = data.page.total;
				} else {
					this.dataList = [];
					this.totalPage = 0;
				}
				this.dataListLoading = false;
			});
		},
		// 每页数
		sizeChangeHandle(val) {
			this.pageSize = val;
			this.pageIndex = 1;
			this.getDataList();
		},
		// 当前页
		currentChangeHandle(val) {
			this.pageIndex = val;
			this.getDataList();
		},
		// 多选
		selectionChangeHandle(val) {
			this.dataListSelections = val;
		},
		// 新增 / 修改
		addOrUpdateHandle() {
			this.addOrUpdateVisible = true;
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init(this.reserveInfo.id);
			});
		},
		// 删除
		deleteHandle(id) {
			var ids = id
				? [id]
				: this.dataListSelections.map(item => {
						return item.id;
				  });
			this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http({
					url: this.$http.adornUrl('/reception/tstorereservedesk/delete'),
					method: 'post',
					data: this.$http.adornData(ids, false)
				}).then(({ data }) => {
					if (data && data.code === 0) {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.getDataList();
							}
						});
					} else {
						this.$message.error(data.msg);
					}
				});
			});
		}
	}
};
</script>
